package com.qf.ssm.service.impl;

import com.qf.ssm.ex.ServiceException;
import com.qf.ssm.mapper.CompanyMapper;
import com.qf.ssm.po.Company;
import com.qf.ssm.qo.*;
import com.qf.ssm.result.ResultStatus;
import com.qf.ssm.service.CompanyService;
import com.qf.ssm.utlis.FilesUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.File;
import java.io.IOException;

/*
 * 公司客户端
 *
 * */
@Service
public class CompanyServiceImpl implements CompanyService {
    @Autowired
    private CompanyMapper companyMapper;

    @Override
    public Company login(CompanyLoginQO companyLoginQO, HttpServletRequest request) {
        Company company = companyMapper.selectByCompanyAccount(companyLoginQO.getCompanyAccount());
        if(company==null||!companyLoginQO.getCompanyPassword().equals(company.getCompanyPassword())){
            throw new ServiceException(ResultStatus.USER_LOGIN_ERROR);
        }
        //登录成功后将用户对象存于session
        request.getSession().setAttribute("company",company);
        return company;
    }

    @Override
    public Integer register(CompanyRegisterQO companyRegisterQO,HttpServletRequest request,MultipartFile[] file)  {
        try {
            companyRegisterQO.setCarRentalLicense(FilesUtils.fileUpload(request,file));
        }catch (IOException ioException){
            throw new ServiceException(ResultStatus.FILE_UP_ERROR);
        }
        int count = companyMapper.insert(companyRegisterQO);
        if (count<1){
            throw new ServiceException(ResultStatus.USER_EXIST);
        }
        return count;
    }

    @Override
    public Integer modifyPassword(CompanyUpdatePasswordQO companyUpdatePasswordQO, HttpServletRequest request) {
        //从session获取用户id
        Company company = (Company) request.getSession().getAttribute("company");
        if(company==null){
            throw new ServiceException(ResultStatus.GET_DATA_ERROR);
        }
        int companyId=company.getCompanyId();
        int count = companyMapper.updatePassword(companyId,companyUpdatePasswordQO.getOldPassword(),companyUpdatePasswordQO.getNewPassword());
        if (count<1){
            throw new ServiceException(ResultStatus.UPDATE_ERROR);
        }
        return count;
    }

    @Override
    public Integer modifyPasswordByEmail(CompanyFindPasswordQO companyFindPasswordQO, HttpServletRequest request) {
        int count = companyMapper.updatePasswordByEmail(companyFindPasswordQO);
        if (count<1){
            throw new ServiceException(ResultStatus.UPDATE_ERROR);
        }
        return count;
    }

    @Override
    public Company queryCompanyInfo(HttpServletRequest request) {
        //从session获取用户对象信息
        Company company = (Company) request.getSession().getAttribute("company");
        if(company==null){
            throw new ServiceException(ResultStatus.GET_DATA_ERROR);
        }
        return company;
    }

    @Override
    public Integer modifyCompanyInfo(CompanyQO newCompany ,HttpServletRequest request) {
        //从session获取用户对象信息
        Company oldCompany = (Company) request.getSession().getAttribute("company");
        if(oldCompany==null){
            throw new ServiceException(ResultStatus.GET_DATA_ERROR);
        }
        //进行修改
        int count = companyMapper.updateCompanyInfo(oldCompany.getCompanyId(),newCompany);
        if(count<1){
            throw new  ServiceException(ResultStatus.UPDATE_ERROR);
        }
        //修改成功后更新session用户对象信息
        Company company = queryCompanyInfo(request);
        request.getSession().setAttribute("company",company);
        return count;
    }

    @Override
    public File queryCarRentalLicense(HttpServletRequest request, HttpServletResponse response) {
        //从session获取用户对象汽车租赁经营许可证信息
        Company company = (Company) request.getSession().getAttribute("company");
        if(company==null){
            throw new ServiceException(ResultStatus.GET_DATA_ERROR);
        }
        //调用工具类
        String carRentalLicense = company.getCarRentalLicense();
        try {
            FilesUtils.download(carRentalLicense,request.getSession(),response);
        }catch (IOException ioException){
            throw new ServiceException(ResultStatus.FILE_UP_ERROR);
        }
        return null;
    }

    @Override
    public Integer loginOut(HttpServletRequest request) {
        //退出登录将存于session的用户对象删除
        HttpSession session = request.getSession();
        session.removeAttribute("company");
        return 1;
    }
}
